$(function () {
    ghck_init();
})


function ghck_init() {
    $("[name='ghck_sbgs']").jgbmtree($("[name='xsd_sbbm']"), $("[name='xsd_sbr']"), '', '')
    //客户加载
    $("[name='ghck_kh']").qyjgzcombobox();
    //工程加载
    $("[name='ghck_gc']").cxgccombobox();
    var options = getGhckGridOption();
    $('#ghck-grid').datagrid(options);
}

function ghck_search() {
    var zt = $("#ghck-toolbar input[name='ghck_zt']").val();
    var xsdh = $("#ghck-toolbar input[name='ghck_xsdh']").val();
    var kh = $("#ghck-toolbar input[name='ghck_kh']").val();
    var sbgs = $("#ghck-toolbar input[name='ghck_sbgs']").val();
    var sbbm = $("#ghck-toolbar input[name='ghck_sbbm']").val();
    var sbr = $("#ghck-toolbar input[name='ghck_sbr']").val();
    var gc = $("#ghck-toolbar input[name='ghck_gc']").val();
    var fhsjStart = $('#ghck_fhrq_start').datebox('getValue');
    var fhsjEnd = $('#ghck_fhrq_end').datebox('getValue');
    var dlkhxsd = ($("#ghck_dlkhxsd").attr("checked") == 'checked');
    var options = $.extend(getGhckGridOption(),
        {pageNumber: 1, queryParams: {zt: zt, xsdh: xsdh, khId: kh, sbgsId: sbgs, sbbmId: sbbm, sbrId: sbr, gcId: gc, fhsjStart: fhsjStart, fhsjEnd: fhsjEnd, dlkhxsd: dlkhxsd}});
    $('#ghck-grid').datagrid(options);
}


// 过户
/*function ghck_gh() {
 var row = $('#ghck-grid').datagrid('getSelected');
 if (row) {
 $.messager.confirm('确认', '是否执行过户', function (r) {
 if (r) {
 $("#main_index_center").mask();
 $.post($.webPath() + '/xsgl/xsdgl/commit_xsd?comType=transfer', {xsdId: row.id}, function (data) {
 var obj = eval('(' + data + ')');
 if (obj.statusCode == '200') {
 $("#main_index_center").mask('hide');
 $("#main_index_center").children(".mask").hide();
 $("#main_index_center").children(".mask-msg").hide();
 $('#ghck-grid').datagrid('reload');
 } else {
 $("#main_index_center").mask('hide');
 $("#main_index_center").children(".mask").hide();
 $("#main_index_center").children(".mask-msg").hide();
 //                        $("#xsd-commit").parent().parent().parent().parent().parent().mask('hide');
 $('#ghck-grid').datagrid('reload');
 $.messager.alert('提示', obj.message, 'info');
 }

 });
 }
 });
 } else {
 $.messager.alert('提示', "请选择要过户的销售单", 'info');
 }
 }*/

var cksh;
// 出库金额审核
function gkck_sh() {
    var record = $("#ghck-grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要挂款审核的记录!", "info");
        return;
    }
    var buttons = [
        {text: '确认', handler: gksh_saveItem}
    ];

    cksh = $.window({
        title: '挂款金额审核',
        iconCls: 'icon-add2',
        url: $.webPath() + "/xsgl/xsdgl/gksh?xsdId=" + record.id,
        width: 424,
        height: 250,
        buttons: buttons
    })

}

function gksh_saveItem() {
    $.messager.progress();
    $("#gkshForm").form('submit', {
        url: $.webPath() + "/xsgl/xsdgl/gksh_saveItem",
        onSubmit: function (param) {
        },
        success: function (data) {
            $.messager.progress('close');
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(cksh).window('close');
                $('#ghck-grid').datagrid('reload');
                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
            } else {
                $("cksh").mask('hide');
                $.messager.alert('提示', obj.message, 'info');
            }
        }
    });
}

// 出库
function ghck_self() {
    var row = $('#ghck-grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('确认', '是否执行放货操作?', function (r) {
            if (r) {
                $("#main_index_center").mask();
                $.post($.webPath() + '/xsgl/xsdgl/commit_xsd?comType=self', {xsdId: row.id}, function (data) {
                    var obj = eval('(' + data + ')');
                    $("#main_index_center").mask('hide');
                    $("#main_index_center").children(".mask").hide();
                    $("#main_index_center").children(".mask-msg").hide();
                    $('#ghck-grid').datagrid('reload');
                    $.messager.alert('提示', obj.message, 'info');
                });
            }
        });
    } else {
        $.messager.alert('提示', "请选择要放货的销售单", 'info');
    }
}

function getGhckGridOption() {
    var options = {
        url: $.webPath() + "/xsgl/xsdgl/ghckList?sftzd=false",
        toolbar: "#ghck-toolbar",
        fitColumns: true,
        singleSelect: true,
        pageNumber: 1,
        pagination: true,
        fit: true,
        rownumbers: true,
        showFooter: true,
        onLoadSuccess: function (target) {
            $(this).datagrid('selectRow', 0);
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.xslx) {
                    this.xslx = this.xslx == 'htxs' ? "合同销售" : this.xslx == 'zddh' ? "直达到货" : this.xslx == 'bgxs' ? "保供销售" : "库存零售";
                }
            })
            return data;
        },
        onSelect: function (rowIndex, rowData) {
            if (!rowData) {
                $("#gchk_commit").hide();
                $("#ghck_self").hide();
                return;
            }
            if (rowData && rowData.zt == 'ygk') {
                $("#ghck_commit").show();
                $("#ghck_self").hide();
            } else if (rowData && rowData.zt == 'ytj') {
                $("#ghck_commit").hide();
                $("#ghck_self").show();
            } else {
                $("#ghck_commit").hide();
                $("#ghck_self").hide();
            }
        }, onDblClickCell: function (index, field) {
            if (field == "xsdh") {
                $("#xsd-edit:visible").click();
            }
        }
    }

    var columns = {columns: [
        [
            {field: 'xsdh', title: '销售单号', align: 'center', width: 50, formatter: xsdCkGlCell},
            {field: 'xslx', title: '销售类型', align: 'center', width: 30},
            {field: 'fhsj', title: '发货日期', align: 'center', width: 35},
            {field: 'sxsj', title: '制单日期', align: 'center', width: 35},
            {field: 'kh_jc', title: '客户名称', align: 'center', width: 50},
            {field: 'xsjs', title: '销售件数', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }
            },
            {field: 'xssl', title: '销售重量', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }
            },
            {field: 'xsje', title: '销售总金额(元)', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }
            },
            {field: 'fyzr_z', title: '配送类型', align: 'center', width: 25},
            {field: 'sbr_xm', title: '申报人', align: 'center', hidden: true},
            {field: 'orderId', title: '来源类型', align: 'center', width: 25, formatter: function (value) {
                if (value)
                    return "线上";
                return "线下";
            }},
            {field: 'xsgs_jc', title: '销售公司', align: 'center', width: 30},
            {field: 'sbgs_jc', title: '申报公司', align: 'center', width: 25},
            {field: 'sbbm_jc', title: '申报部门', align: 'center', hidden: true},
            {field: 'sbr_xm', title: '申报人', align: 'center', hidden: true},
            {field: 'zt', title: '状态', width: 20, align: 'center', formatter: function (value, rowData) {
                if (value == 'ygk')
                    return '已挂款';
                if (value == 'ytj')
                    return '已审核';
                if (value == 'ysp')
                    return '已出库';

            }}
        ]
    ]}
    options = $.extend(options, columns);
    return options;
}